package com.sx.micowell.framework.app

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import com.alibaba.android.arouter.launcher.ARouter
import com.danikula.videocache.HttpProxyCacheServer
import com.example.mvvm.base.BaseApplication
import com.example.mvvm.rx.RxTimer
import com.example.mvvm.until.Utils
import com.miloyu.modelnet.cache.AppNet
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.sx.micowell.BuildConfig
import com.sx.micowell.R
import com.sx.micowell.wxapi.WxUtils
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.uuzuche.lib_zxing.activity.ZXingLibrary
import io.reactivex.Observable
import io.reactivex.Scheduler
import io.reactivex.schedulers.Schedulers


/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/6/25 12:04
 */
class MyApplication : BaseApplication() {
    //单例化的第一种方式：声明一个简单的Application属性
    companion object {
        //情况一：声明可空的属性
        private var instance: MyApplication? = null
        fun instance() = instance!!
        //情况二：声明延迟初始化属性
        //private lateinit var instance: MainApplication
        //fun instance() = instance
    }


    override fun onCreate() {
        super.onCreate()
        AppNet.appContext = this
        instance = this;
        if (BuildConfig.DEBUG) {
            // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog() // 打印日志
            ARouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)}
        }
        ARouter.init(this)
        //初始化工具类
        Utils.init(this)

        var rxTIme=RxTimer().timer(100,{
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context: Context?, layout: RefreshLayout ->
                layout.setPrimaryColorsId(R.color.white, R.color.c_333333)
                ClassicsHeader(context)
            }
            // 二维码
            ZXingLibrary.initDisplayOpinion(this)
//            TuyaHomeSdk.init(this)
//            TuyaHomeSdk.setDebugMode(true)
        })

        val wxUtils = WxUtils()
//        AppConstant.DEVICE_TYPE = wxUtils.isWeiXinAvailable
        AppConstant.DEVICE_TYPE = false
    }

    override fun getResources(): Resources {
        val res = super.getResources()
        if (res.configuration.fontScale != 1.0f) { //非默认值
            val newConfig = Configuration()
            newConfig.fontScale = 1.0f
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1f) //非默认值
            resources
        super.onConfigurationChanged(newConfig)
    }

    private var proxy: HttpProxyCacheServer? = null

    fun getProxy(context: Context): HttpProxyCacheServer? {
        val app: MyApplication = context.applicationContext as MyApplication
        return if (app.proxy == null) app.newProxy().also { app.proxy = it } else app.proxy
    }


    private fun newProxy(): HttpProxyCacheServer? {
        return HttpProxyCacheServer(this)
    }

}